﻿function count(obj, times, callback) {
	var $sefl = $(obj);
	var target = (Math.ceil(new Date().getTime() + times) / 1000 >> 0) * 1000;
	var timing = (Math.ceil(times / 1000) * 1000) || (15 * 60 * 1000);
	var timer = null;

	set(timing);
	
	function set(time) {
		var m = toDouble(Math.floor(time / 60 / 1000)).split('');
		var s = toDouble(time / 1000 % 60).split('');
		
		$sefl.find('.cnt_m_1').html(m[0]);
		$sefl.find('.cnt_m_2').html(m[1]);
		$sefl.find('.cnt_s_1').html(s[0]);
		$sefl.find('.cnt_s_2').html(s[1]);
		
		m = m.join('') >> 0;
		s = s.join('') >> 0;
	}
	
	timer = setInterval(function () {
		var curtime = (Math.ceil(new Date().getTime() + times) / 1000 >> 0) * 1000;

		timing = curtime - target;
		
		set(times - timing);
		
		if (timing === times) {
			callback && callback();
			clearInterval(timer);
		}
	}, 1000);
}

function toDouble(n) {
	return (n >> 0) < 10 ? ('0' + n) : (n + '');
}